{{template "base" .}}

{{define "content"}}
<div x-data="customersData()" class="space-y-6">

    <!-- 状态更新弹窗 -->
    <div x-show="statusDialog.show" 
          class="fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4"
         x-transition:enter="transition ease-out duration-300"
         x-transition:enter-start="opacity-0"
         x-transition:enter-end="opacity-100"
         x-transition:leave="transition ease-in duration-200"
         x-transition:leave-start="opacity-100"
         x-transition:leave-end="opacity-0"
         @click.self="handleCancel()">
        <div class="bg-white rounded-lg shadow-xl max-w-md w-full">
            <!-- 弹窗头部 -->
            <div class="flex items-center justify-between p-6 border-b border-gray-200">
                <div class="flex items-center space-x-3">
                    <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center">
                        <svg class="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
                        </svg>
                    </div>
                    <div>
                        <h3 class="text-lg font-semibold text-gray-900">更新状态</h3>
                    </div>
                </div>
                <button @click="handleCancel()" 
                        class="text-gray-400 hover:text-gray-600 transition-colors p-1 rounded-full hover:bg-gray-100">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                    </svg>
                </button>
            </div>
            
            <!-- 弹窗内容 -->
            <div class="p-6">
                <!-- 客户信息 -->
                <div class="mb-6 p-4 bg-gray-50 rounded-lg">
                    <div class="flex items-center space-x-3">
                        <div class="w-12 h-12 bg-gradient-to-r from-blue-500 to-purple-600 rounded-full flex items-center justify-center text-white font-bold text-lg"
                             x-text="statusDialog.customer?.name?.charAt(0) || ''"></div>
                        <div>
                            <h4 class="font-medium text-gray-900" x-text="statusDialog.customer?.name || ''"></h4>
                            <p class="text-sm text-gray-500">
                                当前状态：<span class="font-medium text-blue-600" x-text="getStatusText(statusDialog.customer?.status)"></span>
                            </p>
                        </div>
                    </div>
                </div>
                
                <!-- 状态选择 -->
                <div class="space-y-3">
                    <label class="block text-sm font-medium text-gray-700 mb-3">选择新状态</label>
                    <div class="grid grid-cols-1 gap-2">
                        <template x-for="option in statusOptions" :key="option.value">
                            <label class="relative flex items-center p-3 border rounded-lg cursor-pointer hover:bg-gray-50 transition-colors"
                                   :class="statusDialog.newStatus === option.value ? 'border-blue-500 bg-blue-50' : 'border-gray-200'">
                                <input type="radio" 
                                       x-model="statusDialog.newStatus" 
                                       :value="option.value" 
                                       class="sr-only"
                                       @change="saveDraft()">
                                <div class="flex items-center space-x-3 w-full">
                                    <div class="w-4 h-4 border-2 rounded-full flex items-center justify-center"
                                         :class="statusDialog.newStatus === option.value ? 'border-blue-500 bg-blue-500' : 'border-gray-300'">
                                        <div x-show="statusDialog.newStatus === option.value" 
                                             class="w-2 h-2 bg-white rounded-full"></div>
                                    </div>
                                    <div class="flex items-center space-x-2">
                                        <span class="w-2 h-2 rounded-full" :class="option.color"></span>
                                        <span class="text-sm font-medium" x-text="option.label"></span>
                                    </div>
                                </div>
                            </label>
                        </template>
                    </div>
                </div>
            </div>
            
            <!-- 弹窗底部 -->
            <div class="flex items-center justify-end space-x-3 p-6 border-t border-gray-200 bg-gray-50 rounded-b-lg">
                <button @click="handleCancel()" 
                        class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 transition-colors">
                    取消
                </button>
                <button @click="confirmStatusUpdate()" 
                        class="px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 transition-colors">
                    <span>确认更新</span>
                </button>
            </div>
        </div>
    </div>

    <!-- 取消编辑确认对话框 -->
    <div x-show="statusDialog.showCancelDialog" 
         class="fixed inset-0 bg-black bg-opacity-50 z-60 flex items-center justify-center p-4"
         x-transition:enter="transition ease-out duration-300"
         x-transition:enter-start="opacity-0"
         x-transition:enter-end="opacity-100"
         x-transition:leave="transition ease-in duration-200"
         x-transition:leave-start="opacity-100"
         x-transition:leave-end="opacity-0">
        <div class="bg-white rounded-lg shadow-xl max-w-md w-full">
            <div class="p-6">
                <div class="flex items-center space-x-3 mb-4">
                    <div class="w-10 h-10 bg-yellow-100 rounded-full flex items-center justify-center">
                        <svg class="w-6 h-6 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
                        </svg>
                    </div>
                    <div>
                        <h3 class="text-lg font-semibold text-gray-900">确认关闭</h3>
                        <p class="text-sm text-gray-500">您的修改尚未保存</p>
                    </div>
                </div>
                
                <p class="text-gray-600 mb-6">
                    您对状态的修改尚未保存，确定要关闭吗？
                </p>
                
                <div class="flex items-center justify-end space-x-3">
                    <button @click="cancelCancel()" 
                            class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 transition-colors">
                        继续编辑
                    </button>
                    <button @click="handleCancel()" 
                            class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-md hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 transition-colors">
                        确认关闭
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Page Header -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">客户管理</h1>
            <p class="mt-1 text-sm text-gray-500">管理您的客户信息，跟踪客户状态和推送记录。</p>
        </div>
        <div class="mt-4 sm:mt-0 flex space-x-2">
            <button @click="exportCustomers()" 
                    class="inline-flex items-center px-3 py-1.5 border border-gray-300 rounded-md shadow-sm text-xs font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-1 focus:ring-blue-500">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                </svg>
                导出数据
            </button>
            <a href="/customers/new" 
               class="inline-flex items-center px-3 py-1.5 border border-transparent rounded-md shadow-sm text-xs font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-1 focus:ring-blue-500">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                </svg>
                添加客户
            </a>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="flex gap-4 overflow-x-auto stats-grid">
        <!-- 统计卡片公共样式 -->
        <template x-data="{ 
            cards: [
                { key: 'total', label: '总客户', gradient: 'from-blue-500 to-blue-600', textColor: 'text-blue-100', icon: 'M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z' },
                { key: 'pending', label: '待处理', gradient: 'from-green-500 to-green-600', textColor: 'text-green-100', icon: 'M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z' },
                { key: 'interested', label: '有意向', gradient: 'from-purple-500 to-purple-600', textColor: 'text-purple-100', icon: 'M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z' },
                { key: 'enrolled', label: '已报名', gradient: 'from-indigo-500 to-indigo-600', textColor: 'text-indigo-100', icon: 'M12 14l9-5-9-5-9 5 9 5z M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z' }
            ]
        }">
            <template x-for="card in cards" :key="card.key">
                <div class="rounded-xl p-4 text-white shadow-lg hover:shadow-xl transition-all duration-300 flex-shrink-0 min-w-[140px] transform hover:scale-105 bg-gradient-to-r" :class="card.gradient">
                    <div class="flex items-center justify-between">
                        <div>
                            <div class="text-sm font-medium" :class="card.textColor" x-text="card.label"></div>
                            <div class="text-2xl font-bold mt-1" x-text="stats[card.key] || '0'"></div>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-full p-3">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" :d="card.icon"></path>
                            </svg>
                        </div>
                    </div>
                </div>
            </template>
        </template>
    </div>

    <!-- Search and Filter -->
    <div class="bg-white shadow rounded-lg search-container">
        <div class="p-6">
            <!-- 搜索和筛选区域 - 标签和表单水平排列 -->
            <div class="space-y-4">
                <!-- 搜索和筛选表单 -->
                <div class="flex flex-wrap items-center gap-4">
                    <div class="flex items-center space-x-2">
                        <label for="search" class="text-xs font-medium text-gray-700 whitespace-nowrap">搜索:</label>
                        <div class="relative">
                            <input type="text" 
                                   id="search"
                                   x-model="filters.search" 
                                   @input="debounceSearch()"
                                   placeholder="姓名或手机号" 
                                   class="block w-32 pl-6 pr-2 py-1.5 text-xs border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-blue-500 focus:border-blue-500">
                            <div class="absolute inset-y-0 left-0 pl-1.5 flex items-center pointer-events-none">
                                <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
                                </svg>
                            </div>
                        </div>
                    </div>
                    
                    <div class="flex items-center space-x-2">
                        <label for="status" class="text-xs font-medium text-gray-700 whitespace-nowrap">状态:</label>
                        <select id="status" 
                                x-model="filters.status" 
                                @change="pagination.page = 1; loadCustomers()"
                                class="block w-20 px-1.5 py-1.5 text-xs border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
                            <option value="">全部</option>  
                            <option value="interested">有意向</option>
                            <option value="enrolled">已报名</option>
                            <option value="invalid">无效</option>
                        </select>
                    </div>
                    
                    <div class="flex items-center space-x-2">
                        <label for="city" class="text-xs font-medium text-gray-700 whitespace-nowrap">城市:</label>
                        <select id="city" 
                                x-model="filters.city" 
                                @change="pagination.page = 1; loadCustomers()"
                                class="block w-20 px-1.5 py-1.5 text-xs border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
                            <option value="">全部</option>
                            <template x-for="city in cities" :key="city">
                                <option :value="city" x-text="city"></option>
                            </template>
                        </select>
                    </div>
                    
                    <div class="flex items-center space-x-2">
                        <label for="subject" class="text-xs font-medium text-gray-700 whitespace-nowrap">科目:</label>
                        <select id="subject" 
                                x-model="filters.subject" 
                                @change="pagination.page = 1; loadCustomers()"
                                class="block w-20 px-1.5 py-1.5 text-xs border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
                            <option value="">全部</option>
                            <template x-for="subject in subjects" :key="subject">
                                <option :value="subject" x-text="subject"></option>
                            </template>
                        </select>
                    </div>
                    
                    <div class="flex items-center space-x-2">
                        <label for="start_date" class="text-xs font-medium text-gray-700 whitespace-nowrap">开始:</label>
                        <input type="date" 
                               id="start_date"
                               x-model="filters.start_date" 
                               @change="pagination.page = 1; loadCustomers()"
                               class="block w-32 px-2 py-1.5 text-xs border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    
                    <div class="flex items-center space-x-2">
                        <label for="end_date" class="text-xs font-medium text-gray-700 whitespace-nowrap">结束:</label>
                        <input type="date" 
                               id="end_date"
                               x-model="filters.end_date" 
                               @change="pagination.page = 1; loadCustomers()"
                               class="block w-32 px-2 py-1.5 text-xs border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    
                    <div class="flex space-x-2 ml-auto">
                        <button @click="debounceSearch()" 
                                class="inline-flex items-center px-3 py-1.5 border border-transparent rounded-md shadow-sm text-xs font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-1 focus:ring-blue-500">
                            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
                            </svg>
                            搜索
                        </button>
                        <button @click="resetFilters()" 
                                class="inline-flex items-center px-3 py-1.5 border border-gray-300 rounded-md shadow-sm text-xs font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-1 focus:ring-blue-500">
                            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>
                            </svg>
                            重置
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Customer List -->
    <div class="bg-white shadow rounded-lg">
        <div class="px-6 py-4 border-b border-gray-200">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-medium text-gray-900">客户列表</h3>
                    <p class="text-sm text-gray-500">
                    管理和查看所有客户信息 · 共 <span class="font-medium text-blue-600" x-text="pagination.total">0</span> 条记录
                    </p>
                </div>
                <div class="flex items-center space-x-3">
                    共 <span class="font-medium text-blue-600" x-text="pagination.total">0</span> 条记录
                </div>
                
            </div>
        </div>
        
        <!-- 加载状态骨架屏 -->
        <div x-show="loading" class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            <div class="h-4 w-4 bg-gray-200 rounded animate-pulse"></div>
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            客户信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            联系方式
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            学习信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            备注信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            状态
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            创建时间
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <!-- 骨架屏行 -->
                    <template x-for="i in 5" :key="i">
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="h-4 w-4 bg-gray-200 rounded animate-pulse"></div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="flex-shrink-0">
                                        <div class="h-12 w-12 bg-gray-200 rounded-full animate-pulse"></div>
                                    </div>
                                    <div class="ml-4 space-y-2">
                                        <div class="h-4 w-24 bg-gray-200 rounded animate-pulse"></div>
                                        <div class="h-3 w-16 bg-gray-200 rounded animate-pulse"></div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="space-y-2">
                                    <div class="h-4 w-28 bg-gray-200 rounded animate-pulse"></div>
                                    <div class="h-3 w-20 bg-gray-200 rounded animate-pulse"></div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="space-y-2">
                                    <div class="h-4 w-16 bg-gray-200 rounded animate-pulse"></div>
                                    <div class="h-3 w-20 bg-gray-200 rounded animate-pulse"></div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="h-6 w-16 bg-gray-200 rounded-full animate-pulse"></div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="h-4 w-24 bg-gray-200 rounded animate-pulse"></div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex space-x-2">
                                    <div class="h-6 w-6 bg-gray-200 rounded animate-pulse"></div>
                                    <div class="h-6 w-6 bg-gray-200 rounded animate-pulse"></div>
                                    <div class="h-6 w-6 bg-gray-200 rounded animate-pulse"></div>
                                </div>
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>
        
        <div x-show="!loading && customers.length === 0" class="text-center py-12">
            <div class="flex flex-col items-center">
                <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mb-4">
                    <svg class="w-8 h-8 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z"></path>
                    </svg>
                </div>
                <h3 class="text-lg font-medium text-gray-900 mb-2">暂无客户数据</h3>
                <p class="text-gray-500 mb-4">还没有添加任何客户，点击下方按钮开始添加</p>
                <a href="/customers/new" 
                   class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700">
                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                    </svg>
                    添加客户
                </a>
            </div>
        </div>
        
        <!-- 表格视图 -->
        <div x-show="viewMode === 'table' && !loading && customers.length > 0" class="overflow-x-auto table-container">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            <input type="checkbox" @change="toggleSelectAll($event)" 
                                   class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            客户信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            联系方式
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            学习信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            备注信息
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            状态
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            创建时间
                        </th>
                        <th scope="col" class="px-6 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <template x-for="customer in customers" :key="customer.id">
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <input type="checkbox" :value="customer.id" x-model="selectedCustomers" 
                                       class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="flex-shrink-0">
                                        <div class="h-12 w-12 rounded-full bg-gradient-to-r from-blue-400 to-blue-600 flex items-center justify-center shadow-md">
                                            <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                                            </svg>
                                        </div>
                                    </div>
                                    <div class="ml-4">
                                        <div class="text-sm font-semibold text-gray-900" x-text="customer.name"></div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-gray-900 flex items-center flex-nowrap">
                                    <svg class="w-4 h-4 text-gray-400 mr-2 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"></path>
                                    </svg>
                                    <span x-text="customer.phone" class="whitespace-nowrap"></span>
                                </div>
                                <div class="text-sm text-gray-500 flex items-center flex-nowrap">
                                    <svg class="w-4 h-4 text-gray-400 mr-2 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                    </svg>
                                    <span x-text="customer.city" class="whitespace-nowrap"></span>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-gray-900 flex items-center flex-nowrap">
                                    <svg class="w-4 h-4 text-gray-400 mr-2 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path>
                                    </svg>
                                    <span x-text="customer.subject" class="whitespace-nowrap"></span>
                                </div>
                                <div class="text-sm text-gray-500 flex items-center flex-nowrap">
                                    <svg class="w-4 h-4 text-gray-400 mr-2 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5z"></path>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
                                    </svg>
                                    <span x-text="customer.grade" class="whitespace-nowrap"></span>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <div class="text-sm text-gray-500" x-data="{ expanded: false }">
                                    <div class="flex items-center">
                                        <svg class="w-4 h-4 text-gray-400 mr-2 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-1.586l-4 4z"></path>
                                        </svg>
                                        <template x-if="customer.notes && customer.notes.length > 30">
                                            <div class="flex-1">
                                                <span x-show="!expanded" x-text="customer.notes.substring(0, 30) + '...'" class="cursor-pointer hover:text-gray-700" @click="expanded = true"></span>
                                                <span x-show="expanded" x-text="customer.notes" class="cursor-pointer hover:text-gray-700" @click="expanded = false"></span>
                                            </div>
                                        </template>
                                        <template x-if="!customer.notes || customer.notes.length <= 30">
                                            <span x-text="customer.notes || '暂无备注'" class="flex-1"></span>
                                        </template>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="inline-flex px-3 py-1 text-xs font-semibold rounded-full shadow-sm"
                                      :class="getStatusClass(customer.status)"
                                      x-text="getStatusText(customer.status)"></span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <div class="flex items-center">
                                    <svg class="w-4 h-4 text-gray-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
                                    </svg>
                                    <span x-text="formatDate(customer.created_at)"></span>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <div class="flex flex-col space-y-1">
                                    <!-- 主要操作 -->
                                    <div class="flex space-x-1">
                                        <button @click="viewCustomer(customer)" 
                                                class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-blue-600 hover:text-blue-900 hover:bg-blue-50" 
                                                title="查看详情">
                                            <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                            </svg>
                                            查看
                                        </button>
                                        <a :href="'/customers/' + customer.id + '/edit'" 
                                           class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-green-600 hover:text-green-900 hover:bg-green-50" 
                                           title="编辑客户" target="_blank">
                                            <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                                            </svg>
                                            编辑
                                        </a>
                                    </div>
                                    
                                    <!-- 次要操作 -->
                                    <div class="flex space-x-1">
                                        <button @click="pushCustomer(customer)" 
                                                :disabled="!canPush(customer.status)"
                                                class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-purple-600 hover:text-purple-900 hover:bg-purple-50 disabled:text-gray-400 disabled:hover:bg-transparent" 
                                                title="推送客户">
                                            <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
                                            </svg>
                                            推送
                                        </button>
                                        <button @click="updateStatus(customer)" 
                                                class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-yellow-600 hover:text-yellow-900 hover:bg-yellow-50"
                                                title="更新状态">
                                            <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
                                            </svg>
                                            状态
                                        </button>
                                    </div>
                                    
                                    <!-- 危险操作 -->
                                    <button @click="deleteCustomer(customer.id)" 
                                            class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-red-600 hover:text-red-900 hover:bg-red-50" 
                                            title="删除客户">
                                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                        </svg>
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>
        
        <!-- 卡片视图 -->
        <div x-show="viewMode === 'card' && !loading && customers.length > 0" class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                <template x-for="customer in customers" :key="customer.id">
                    <div class="bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow duration-200">
                        <!-- 卡片头部 -->
                        <div class="p-4 border-b border-gray-100">
                            <div class="flex items-center space-x-3">
                                <div class="flex-shrink-0">
                                    <div class="h-10 w-10 rounded-lg bg-gradient-to-r from-blue-500 to-purple-600 flex items-center justify-center text-white font-bold" 
                                         x-text="customer.name.charAt(0)"></div>
                                </div>
                                <div class="flex-1 min-w-0">
                                    <h4 class="text-sm font-medium text-gray-900 truncate" x-text="customer.name"></h4>
                                </div>
                                <div class="flex-shrink-0">
                                    <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full shadow-sm"
                                          :class="getStatusClass(customer.status)"
                                          x-text="getStatusText(customer.status)"></span>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 卡片内容 -->
                        <div class="p-4 space-y-3">
                            <!-- 联系信息 -->
                            <div class="space-y-2">
                                <div class="text-xs text-gray-500 font-medium uppercase tracking-wide">联系信息</div>
                                <div class="space-y-1">
                                    <div class="text-sm text-gray-900 flex items-center flex-nowrap">
                                        <svg class="w-3 h-3 mr-2 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"></path>
                                        </svg>
                                        <span x-text="customer.phone" class="whitespace-nowrap"></span>
                                    </div>
                                    <div class="text-sm text-gray-600 flex items-center flex-nowrap">
                                        <svg class="w-3 h-3 mr-2 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path>
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                        </svg>
                                        <span x-text="customer.city" class="whitespace-nowrap"></span>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- 学习信息 -->
                            <div class="space-y-2">
                                <div class="text-xs text-gray-500 font-medium uppercase tracking-wide">学习信息</div>
                                <div class="space-y-1">
                                    <div class="text-sm text-gray-900 flex items-center flex-nowrap">
                                        <svg class="w-3 h-3 mr-2 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path>
                                        </svg>
                                        <span x-text="customer.subject" class="whitespace-nowrap"></span>
                                    </div>
                                    <div class="text-sm text-gray-600 flex items-center flex-nowrap">
                                        <svg class="w-3 h-3 mr-2 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5z"></path>
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
                                        </svg>
                                        <span x-text="customer.grade" class="whitespace-nowrap"></span>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- 备注信息 -->
                            <div class="space-y-2">
                                <div class="text-xs text-gray-500 font-medium uppercase tracking-wide">备注信息</div>
                                <div class="text-sm text-gray-600" x-data="{ expanded: false }">
                                    <div class="flex items-start">
                                        <svg class="w-3 h-3 mr-2 text-gray-400 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-1.586l-4 4z"></path>
                                        </svg>
                                        <template x-if="customer.notes && customer.notes.length > 50">
                                            <div class="flex-1">
                                                <span x-show="!expanded" x-text="customer.notes.substring(0, 50) + '...'" class="cursor-pointer hover:text-gray-800" @click="expanded = true"></span>
                                                <span x-show="expanded" x-text="customer.notes" class="cursor-pointer hover:text-gray-800" @click="expanded = false"></span>
                                            </div>
                                        </template>
                                        <template x-if="!customer.notes || customer.notes.length <= 50">
                                            <span x-text="customer.notes || '暂无备注'" class="flex-1"></span>
                                        </template>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- 时间信息 -->
                            <div class="pt-2 border-t border-gray-100">
                                <div class="text-xs text-gray-500 flex items-center">
                                    <svg class="w-3 h-3 mr-1 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
                                    </svg>
                                    创建于 <span x-text="formatDate(customer.created_at)" class="ml-1"></span>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 卡片操作 -->
                        <div class="px-4 py-3 bg-gray-50 border-t border-gray-100 rounded-b-lg">
                            <div class="flex items-center justify-between">
                                <div class="flex space-x-2">
                                    <button @click="viewCustomer(customer)" 
                                            class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-blue-600 hover:text-blue-900 hover:bg-blue-100" 
                                            title="查看详情">
                                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                        </svg>
                                        查看
                                    </button>
                                    <a :href="'/customers/' + customer.id + '/edit'" 
                                       class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-green-600 hover:text-green-900 hover:bg-green-100" 
                                       title="编辑客户" target="_blank">
                                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                                        </svg>
                                        编辑
                                    </a>
                                </div>
                                <div class="flex space-x-1">
                                    <button @click="pushCustomer(customer)" 
                                            :disabled="!canPush(customer.status)"
                                            class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-purple-600 hover:text-purple-900 hover:bg-purple-100 disabled:text-gray-400 disabled:hover:bg-transparent" 
                                            title="推送客户">
                                        <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
                                        </svg>
                                    </button>
                                    <button @click="deleteCustomer(customer.id)" 
                                            class="inline-flex items-center px-2 py-1 text-xs font-medium rounded text-red-600 hover:text-red-900 hover:bg-red-100" 
                                            title="删除客户">
                                        <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                        </svg>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </template>
            </div>
        </div>

        <!-- Pagination -->
        <div x-show="!loading && customers.length > 0" class="bg-white px-6 py-4 border-t border-gray-200">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between space-y-3 sm:space-y-0">
                <div class="flex items-center text-sm text-gray-700">
                    <span>显示第 </span>
                    <span class="font-semibold text-blue-600 mx-1" x-text="(pagination.page - 1) * pagination.limit + 1"></span>
                    <span> 到 </span>
                    <span class="font-semibold text-blue-600 mx-1" x-text="Math.min(pagination.page * pagination.limit, pagination.total)"></span>
                    <span> 条，共 </span>
                    <span class="font-semibold text-blue-600 mx-1" x-text="pagination.total"></span>
                    <span> 条记录</span>
                </div>
                
                <div class="flex items-center space-x-2">
                    <!-- 移动端分页 -->
                    <div class="flex sm:hidden space-x-1">
                        <button @click="previousPage()" :disabled="pagination.page <= 1" 
                                class="relative inline-flex items-center px-2 py-1 border border-gray-300 text-xs font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-150">
                            <i class="fas fa-chevron-left mr-1 text-sm"></i>
                            上一页
                        </button>
                        <button @click="nextPage()" :disabled="pagination.page >= pagination.pages" 
                                class="relative inline-flex items-center px-2 py-1 border border-gray-300 text-xs font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-150">
                            下一页
                            <i class="fas fa-chevron-right ml-1 text-sm"></i>
                        </button>
                    </div>
                    
                    <!-- 桌面端分页 -->
                    <nav class="hidden sm:flex items-center space-x-1">
                        <button @click="previousPage()" :disabled="pagination.page <= 1" 
                                class="relative inline-flex items-center px-2 py-1 rounded-md border border-gray-300 bg-white text-xs font-medium text-gray-500 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-150">
                            <i class="fas fa-chevron-left text-sm"></i>
                        </button>
                        
                        <template x-for="page in getPageNumbers()" :key="page">
                            <button @click="goToPage(page)" 
                                    :class="page === pagination.page ? 'bg-blue-600 border-blue-600 text-white shadow-md' : 'bg-white border-gray-300 text-gray-700 hover:bg-gray-50'"
                                    class="relative inline-flex items-center px-2 py-1 border text-xs font-medium rounded-md transition-colors duration-150"
                                    x-text="page"></button>
                        </template>
                        
                        <button @click="nextPage()" :disabled="pagination.page >= pagination.pages" 
                                class="relative inline-flex items-center px-2 py-1 rounded-md border border-gray-300 bg-white text-xs font-medium text-gray-500 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-150">
                            <i class="fas fa-chevron-right text-sm"></i>
                        </button>
                    </nav>
                </div>
            </div>
        </div>
    </div>

    <!-- Batch Actions -->
    <div x-show="selectedCustomers.length > 0" 
         x-transition:enter="transition ease-out duration-300"
         x-transition:enter-start="opacity-0"
         x-transition:enter-end="opacity-100"
         x-transition:leave="transition ease-in duration-200"
         x-transition:leave-start="opacity-100"
         x-transition:leave-end="opacity-0"
         class="fixed bottom-6 left-1/2 transform -translate-x-1/2 bg-white shadow-2xl rounded-xl border border-gray-200 p-4 z-50">
        <div class="flex items-center space-x-3">
            <div class="flex items-center space-x-2">
                <div class="w-6 h-6 bg-blue-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-check text-blue-600 text-sm"></i>
                </div>
                <span class="text-xs font-medium text-gray-700">
                    已选择 <span class="text-blue-600 font-semibold" x-text="selectedCustomers.length"></span> 个客户
                </span>
            </div>
            
            <div class="flex items-center space-x-2">

                <button @click="batchDelete()" 
                        class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-lg text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-1 focus:ring-red-500 transition-colors duration-150 shadow-md">
                    <i class="fas fa-trash mr-1.5 text-sm"></i>
                    批量删除
                </button>
                <button @click="selectedCustomers = []" 
                        class="p-1.5 text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-lg transition-colors duration-150"
                        title="取消选择">
                    <i class="fas fa-times text-sm"></i>
                </button>
            </div>
        </div>
    </div>
    

</div>

<script>
    function customersData() {
        return {
            loading: false,
            statsLoading: false,
            customers: [],
            selectedCustomers: [],
            statusDialog: {
                show: false,
                customer: null,
                newStatus: '',
                showCancelDialog: false
            },
            
            // 状态配置（合并状态选项和映射）
            statusOptions: [
                { value: 'interested', label: '有意向', color: 'bg-yellow-400', class: 'bg-purple-100 text-purple-800' },
                { value: 'enrolled', label: '已报名', color: 'bg-green-400', class: 'bg-green-100 text-green-800' },
                { value: 'invalid', label: '无效客户', color: 'bg-red-400', class: 'bg-red-100 text-red-800' }
            ],

            // 可推送状态
            PUSHABLE_STATUSES: ['pending', 'interested'],
            stats: {
                total: 0,
                pending: 0,
                interested: 0,
                enrolled: 0,
                invalid: 0
            },
            

            
            // 可推送状态
            PUSHABLE_STATUSES: ['new', 'contacted', 'interested'],
            stats: {
                total: 0,
                new: 0,
                contacted: 0,
                interested: 0,
                enrolled: 0,
                invalid: 0
            },
            filters: {
                search: '',
                status: '',
                city: '',
                subject: '',
                start_date: '',
                end_date: ''
            },
            cities: [],
            subjects: [],
            pagination: {
                page: 1,
                limit: 20,
                total: 0,
                pages: 0
            },
            searchTimeout: null,
            viewMode: 'table', // 默认表格视图
            
            init() {
                // 设置初始加载状态
                this.loading = true;
                this.statsLoading = true;
                
                // 并行加载数据
                Promise.all([
                    this.loadCustomers(),
                    this.loadStats(),
                    this.loadFilters()
                ]).catch(error => {
                    console.error('初始化失败:', error);
                    showToast('页面初始化失败', 'error');
                });
            },
            
            // 通用API请求方法
            async apiRequest(url, options = {}) {
                const token = localStorage.getItem('token');
                const defaultOptions = {
                    headers: { 
                        'Content-Type': 'application/json',
                        ...options.headers
                    }
                };
                
                try {
                    const response = await fetch(url, { ...defaultOptions, ...options });
                    
                    // 如果需要返回原始response对象（用于文件下载等场景）
                    if (options.returnResponse) {
                        return response;
                    }
                    
                    const data = await response.json();
                    
                    if (!response.ok) {
                        throw new Error(data.message || `HTTP ${response.status}`);
                    }
                    
                    return data;
                } catch (error) {
                    console.error('API请求失败:', error);
                    throw error;
                }
            },
            
            async loadCustomers() {
                this.loading = true;
                try {
                    const params = new URLSearchParams({
                        page: this.pagination.page,
                        size: this.pagination.limit,
                        ...this.filters
                    });
                    
                    const data = await this.apiRequest(`/api/v1/customers?${params}`);
                    
                    if (data.code === 200) {
                        this.customers = data.data || [];
                        this.pagination = {
                            ...this.pagination,
                            page: data.page,
                            total: data.total,
                            pages: Math.ceil(data.total / data.size)
                        };
                    }
                } catch (error) {
                    showToast('加载客户列表失败', 'error');
                } finally {
                    this.loading = false;
                }
            },
            
            async loadStats() {
                this.statsLoading = true;
                try {
                    const data = await this.apiRequest('/api/v1/customers/stats');
                    if (data.code === 200) {
                        this.stats = data.data;
                    }
                } catch (error) {
                    console.error('Failed to load stats:', error);
                } finally {
                    this.statsLoading = false;
                }
            },
            
            async loadFilters() {
                try {
                    const data = await this.apiRequest('/api/v1/customers/filters');
                    if (data.code === 200) {
                        this.cities = data.data.cities || [];
                        this.subjects = data.data.subjects || [];
                    }
                } catch (error) {
                    console.error('Failed to load filters:', error);
                }
            },
            
            debounceSearch() {
                clearTimeout(this.searchTimeout);
                this.searchTimeout = setTimeout(() => {
                    this.pagination.page = 1;
                    this.loadCustomers();
                }, 500);
            },
            
            resetFilters() {
                this.filters = {
                    search: '',
                    status: '',
                    city: '',
                    subject: '',
                    start_date: '',
                    end_date: ''
                };
                this.pagination.page = 1;
                this.loadCustomers();
            },
            
            getStatusClass(status) {
                const option = this.statusOptions.find(opt => opt.value === status);
                return option ? option.class : 'bg-gray-100 text-gray-800';
            },
            
            getStatusText(status) {
                const option = this.statusOptions.find(opt => opt.value === status);
                return option ? option.label : '未知';
            },
            
            canPush(status) {
                return this.PUSHABLE_STATUSES.includes(status);
            },
            
            formatDate(dateString) {
                return new Date(dateString).toLocaleDateString('zh-CN');
            },
            
            toggleSelectAll(event) {
                if (event.target.checked) {
                    this.selectedCustomers = this.customers.map(c => c.id);
                } else {
                    this.selectedCustomers = [];
                }
            },
            
            async pushCustomer(customer) {
                if (!this.canPush(customer.status)) {
                    showToast('该客户状态不允许推送', 'warning');
                    return;
                }
                
                // 跳转到推送页面
                window.location.href = `/push/new?customer_id=${customer.id}`;
            },
            
            async deleteCustomer(customerId) {
                const customer = this.customers.find(c => c.id === customerId);
                if (!customer) {
                    showToast('客户信息不存在', 'error');
                    return;
                }
                
                // 确认删除操作
                if (!this.confirmDelete(customer.name)) {
                    return;
                }
                
                try {
                    const data = await this.apiRequest(`/api/v1/customers/${customerId}`, {
                        method: 'DELETE'
                    });
                    
                    if (data.code === 200) {
                        showToast('客户删除成功', 'success');
                        await Promise.all([this.loadCustomers(), this.loadStats()]);
                    } else {
                        showToast(data.message || '删除客户失败', 'error');
                    }
                } catch (error) {
                    showToast('删除客户失败', 'error');
                }
            },
            
            // 确认删除操作
            confirmDelete(customerName) {
                // 第一次确认
                if (!confirm(`确定要删除客户"${customerName}"吗？\n\n注意：删除后将无法恢复！`)) {
                    return false;
                }
                
                // 第二次确认
                const confirmText = prompt('请输入客户姓名以确认删除操作：');
                if (confirmText !== customerName) {
                    if (confirmText !== null) {
                        showToast('输入的姓名不匹配，删除操作已取消', 'warning');
                    }
                    return false;
                }
                
                return true;
            },
            

            
            async batchDelete() {
                if (this.selectedCustomers.length === 0) {
                    showToast('请选择要删除的客户', 'warning');
                    return;
                }
                
                // 确认批量删除操作
                if (!this.confirmBatchDelete()) {
                    return;
                }
                
                try {
                    const data = await this.apiRequest('/api/v1/customers/batch', {
                        method: 'DELETE',
                        body: JSON.stringify({
                            customer_ids: this.selectedCustomers
                        })
                    });
                    
                    if (data.code === 200) {
                        showToast('批量删除成功', 'success');
                        this.selectedCustomers = [];
                        await Promise.all([this.loadCustomers(), this.loadStats()]);
                    } else {
                        showToast(data.message || '批量删除失败', 'error');
                    }
                } catch (error) {
                    showToast('批量删除失败', 'error');
                }
            },
            
            // 确认批量删除操作
            confirmBatchDelete() {
                // 获取选中客户的姓名列表
                const selectedCustomerNames = this.selectedCustomers.map(id => {
                    const customer = this.customers.find(c => c.id === id);
                    return customer ? customer.name : '未知客户';
                });
                
                // 第一次确认
                if (!confirm(`确定要删除选中的 ${this.selectedCustomers.length} 个客户吗？\n\n客户列表：\n${selectedCustomerNames.join('\n')}\n\n注意：删除后将无法恢复！`)) {
                    return false;
                }
                
                // 第二次确认
                const confirmText = prompt('请输入"确认删除"以继续批量删除操作：');
                if (confirmText !== '确认删除') {
                    if (confirmText !== null) {
                        showToast('输入不匹配，批量删除操作已取消', 'warning');
                    }
                    return false;
                }
                
                return true;
             },
            
            viewCustomer(customer) {
                // 在新页面中打开客户详情页面
                window.open(`/customers/${customer.id}`, '_blank');
            },
            
            // 打开状态更新弹窗
            updateStatus(customer) {
                this.statusDialog.customer = customer;
                this.statusDialog.newStatus = customer.status;
                this.statusDialog.show = true;
                
                // 加载草稿
                this.loadDraft();
            },
            
            // 关闭状态更新弹窗
            closeStatusDialog() {
                this.statusDialog.show = false;
                this.statusDialog.customer = null;
                this.statusDialog.newStatus = '';
                this.statusDialog.showCancelDialog = false;
                this.clearDraft();
            },
            
            // 处理取消操作（合并handleCancel和confirmLeave）
            handleCancel() {
                this.closeStatusDialog();
            },
            
            // 取消离开，继续编辑
            cancelCancel() {
                this.statusDialog.showCancelDialog = false;
            },
            
            // 自动保存草稿
            saveDraft() {
                try {
                    const draftData = {
                        customerId: this.statusDialog.customer?.id,
                        newStatus: this.statusDialog.newStatus,
                        timestamp: Date.now()
                    };
                    localStorage.setItem('customer_status_draft', JSON.stringify(draftData));
                } catch (error) {
                    console.warn('无法保存草稿:', error);
                }
            },
            
            // 加载草稿
            loadDraft() {
                try {
                    const draftData = localStorage.getItem('customer_status_draft');
                    if (draftData) {
                        const draft = JSON.parse(draftData);
                        // 检查草稿是否过期（24小时）和是否匹配当前客户
                        if (Date.now() - draft.timestamp < 24 * 60 * 60 * 1000 && 
                            draft.customerId === this.statusDialog.customer?.id) {
                            this.statusDialog.newStatus = draft.newStatus;
                        } else {
                            this.clearDraft();
                        }
                    }
                } catch (error) {
                    console.warn('无法加载草稿:', error);
                }
            },
            
            // 清除草稿
            clearDraft() {
                try {
                    localStorage.removeItem('customer_status_draft');
                } catch (error) {
                    console.warn('无法清除草稿:', error);
                }
            },
            
            // 确认状态更新
            async confirmStatusUpdate() {
                if (!this.statusDialog.newStatus) {
                    showToast('请选择新状态', 'warning');
                    return;
                }
                
                try {
                    const data = await this.apiRequest(`/api/v1/customers/${this.statusDialog.customer.id}/status`, {
                        method: 'PATCH',
                        body: JSON.stringify({
                            status: this.statusDialog.newStatus
                        })
                    });
                    
                    if (data.code === 200) {
                        showToast('状态更新成功', 'success');
                        this.clearDraft();
                        this.closeStatusDialog();
                        await Promise.all([this.loadCustomers(), this.loadStats()]);
                    } else {
                        showToast(data.message || '状态更新失败', 'error');
                    }
                } catch (error) {
                    showToast('状态更新失败', 'error');
                }
            },
            
            async exportCustomers() {
                try {
                    const params = new URLSearchParams(this.filters);
                    
                    const response = await this.apiRequest(`/api/v1/customers/export?${params}`, {
                        method: 'GET',
                        returnResponse: true // 返回原始response对象用于处理文件下载
                    });
                    
                    if (response.ok) {
                        const blob = await response.blob();
                        const url = window.URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = `customers_${new Date().toISOString().split('T')[0]}.xlsx`;
                        document.body.appendChild(a);
                        a.click();
                        window.URL.revokeObjectURL(url);
                        document.body.removeChild(a);
                        showToast('导出成功', 'success');
                    } else {
                        showToast('导出失败', 'error');
                    }
                } catch (error) {
                    console.error('Failed to export:', error);
                    showToast('导出失败', 'error');
                }
            },
            
            previousPage() {
                if (this.pagination.page > 1) {
                    this.pagination.page--;
                    this.loadCustomers();
                }
            },
            
            nextPage() {
                if (this.pagination.page < this.pagination.pages) {
                    this.pagination.page++;
                    this.loadCustomers();
                }
            },
            
            goToPage(page) {
                this.pagination.page = page;
                this.loadCustomers();
            },
            
            getPageNumbers() {
                const pages = [];
                const current = this.pagination.page;
                const total = this.pagination.pages;
                
                if (total <= 7) {
                    for (let i = 1; i <= total; i++) {
                        pages.push(i);
                    }
                } else {
                    if (current <= 4) {
                        for (let i = 1; i <= 5; i++) {
                            pages.push(i);
                        }
                        pages.push('...');
                        pages.push(total);
                    } else if (current >= total - 3) {
                        pages.push(1);
                        pages.push('...');
                        for (let i = total - 4; i <= total; i++) {
                            pages.push(i);
                        }
                    } else {
                        pages.push(1);
                        pages.push('...');
                        for (let i = current - 1; i <= current + 1; i++) {
                            pages.push(i);
                        }
                        pages.push('...');
                        pages.push(total);
                    }
                }
                
                return pages.filter(p => p !== '...');
            }
        }
    }
</script>
   
{{end}}